<?php
/**
 * Created by PhpStorm.
 * User: iamya
 * Date: 2017/5/25
 * Time: 11:58
 */

namespace app\system\controller;


use app\common\model\ShopCurrencyModel;
use think\Request;

class ShopCurrencyController extends BaseController
{
    protected $model;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->model = new ShopCurrencyModel();
    }

    public function index()
    {
        $list = $this->model->where('site_id',$this->getSiteId())->paginate(10);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->post();
            $data['site_id'] = $this->getSiteId();
            $result = $this->validate($data, 'ShopCurrencyValidate');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->model->addData($data)) {
                $this->success('添加成功', 'index');
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->post();
            $result = $this->validate($data, 'ShopCurrencyValidate');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->model->editData($data)) {
                $this->success('修改成功', 'index');
            } else {
                $this->error('修改失败');
            }
        }
        $id   = $this->request->param('currency_id', 0);
        $info = $this->model->where('currency_id', $id)->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function del()
    {
        $id = $this->request->param('currency_id', 0);
        if ($this->model->delData($id)) {
            $this->success('删除成功', 'index');
        } else {
            $this->error('删除失败');
        };
    }
}